import 'package:flutter/cupertino.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:mziot/app/data/device_list_entity.dart';
import 'package:mziot/app/data/group_down_entity.dart';
import 'package:mziot/app/data/scene_list_entity.dart';
import 'package:mziot/app/tools/tools.dart';

class ChooseSceneController extends GetxController {
  bool? isGroup;
  bool isMutile = false;
  var groupList = <GroupDownEntity>[].obs;
  var sceneList = <SceneListEntity>[].obs;
  var deviceList = <DeviceListList>[].obs;

  dynamic selectModel;
  List<GroupDownEntity> groups = [];
  List<SceneListEntity> scenes = [];
  List<DeviceListList> devices = [];

  void checkSelected() {
    if (isMutile) {
      if (deviceList.isNotEmpty) {
        devices.clear();
        for (int i = 0; i < deviceList.length; i++) {
          if (deviceList[i].isSelected.value) {
            devices.add(deviceList[i]);
          }
        }
        return;
      } else if (groupList.isNotEmpty) {
        groups.clear();
        for (int i = 0; i < groupList.length; i++) {
          if (groupList[i].isSelected.value) {
            groups.add(groupList[i]);
          }
        }
        return;
      } else if (sceneList.isNotEmpty) {
        scenes.clear();
        for (int i = 0; i < sceneList.length; i++) {
          if (sceneList[i].isSelected.value) {
            scenes.add(sceneList[i]);
          }
        }
      }
      return;
    }
    if (!(isGroup ?? false)) {
      groupList.forEach((element) {
        if (element.isSelected.value) {
          selectModel = element;
          return;
        }
      });
    } else {
      if (deviceList.isNotEmpty) {
        deviceList.forEach((element) {
          if (element.isSelected.value) {
            selectModel = element;
            return;
          }
        });
        return;
      }
      sceneList.forEach((element) {
        if (element.isSelected.value) {
          selectModel = element;
          return;
        }
      });
    }
  }

  void selectedAction(int index) {
    if (isMutile) {
      if (deviceList.isNotEmpty) {
        deviceList.value[index].isSelected.value = !deviceList.value[index].isSelected.value;
        devices.clear();
        for (int i = 0; i < deviceList.length; i++) {
          if (deviceList[i].isSelected.value) {
            devices.add(deviceList[i]);
          }
        }
        logger.e('message ${devices.length}');
        return;
      } else if (groupList.isNotEmpty) {
        groupList.value[index].isSelected.value = !groupList.value[index].isSelected.value;
        groups.clear();
        for (int i = 0; i < groupList.length; i++) {
          if (groupList[i].isSelected.value) {
            groups.add(groupList[i]);
          }
        }
        return;
      } else if (sceneList.isNotEmpty) {
        sceneList.value[index].isSelected.value = !sceneList.value[index].isSelected.value;
        scenes.clear();
        for (int i = 0; i < sceneList.length; i++) {
          if (sceneList[i].isSelected.value) {
            scenes.add(sceneList[i]);
          }
        }
      }
      return;
    }
    if (!(isGroup ?? false)) {
      for (int i = 0; i < groupList.length; i++) {
        if (i == index) {
          groupList.value[i].isSelected.value = !groupList.value[i].isSelected.value;
        } else {
          groupList.value[i].isSelected.value = false;
        }
      }
      selectModel = groupList.value[index].isSelected.value ? groupList.value[index] : null;
    } else {
      if (deviceList.isNotEmpty) {
        for (int i = 0; i < deviceList.length; i++) {
          if (i == index) {
            deviceList.value[i].isSelected.value = !deviceList.value[i].isSelected.value;
          } else {
            deviceList.value[i].isSelected.value = false;
          }
        }
        selectModel = deviceList.value[index].isSelected.value ? deviceList.value[index] : null;
        return;
      }
      for (int i = 0; i < sceneList.length; i++) {
        if (i == index) {
          sceneList.value[i].isSelected.value = !sceneList.value[i].isSelected.value;
        } else {
          sceneList.value[i].isSelected.value = false;
        }
      }
      selectModel = sceneList.value[index].isSelected.value ? sceneList.value[index] : null;
    }
  }

  @override
  void onClose() {
    super.onClose();
  }
}

class PopFieldController extends GetxController {
  //TODO: Implement DeviceManagerController
  TextEditingController editingController = TextEditingController();
  FocusNode focusNode = FocusNode();
  final nameText = ''.obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    focusNode.dispose();
    editingController.dispose();
    super.onClose();
  }
}
